package com.spm.librarianmanagersystem.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.spm.librarianmanagersystem.mapper.MemberMapper;
import com.spm.librarianmanagersystem.service.RegisterService;
import com.spm.librarianmanagersystem.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("RegisterService")
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    private MemberMapper mm;
    @Override
    public String register(JSONObject object) {
        try{
            mm.addMember(object.getString("id"),
                    object.getString("password"),
                    object.getString("name"),
                    object.getInteger("gender"),
                    object.getInteger("position"),
                    object.getString("email"),
                    object.getString("phone_number"));
            return TokenUtil.sign(mm.getMemberByID(object.getString("id")));
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Boolean addUser(JSONObject object) {
        try{
            return mm.addMember(object.getString("id"),
                    "123456",
                    object.getString("name"),
                    object.getInteger("gender"),
                    object.getInteger("position"),
                    object.getString("email"),
                    object.getString("phone_number"));
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
